package com.sad.we11cis;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

public class SelectCategory extends Activity {
	
	String selectedCategory;

	private ArrayList<String> mGroupList = null;
	private ArrayList<ArrayList<String>> mChildList = null;
	private ArrayList<String> mChildListContent = null;
	private ArrayList<String> receivedInfo, receivedItem;
	private BaseExpandableAdapter mBaseExpandableAdapter = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_category);
		
		setLayout();

		mGroupList = new ArrayList<String>();
		mChildList = new ArrayList<ArrayList<String>>();
		mChildListContent = new ArrayList<String>();
		
		GetCategoryTask getCategoryTask = new GetCategoryTask();
		getCategoryTask.execute();
		
		/*mGroupList.add("a");
		mGroupList.add("b");
		mGroupList.add("c");
		mGroupList.add("d");
		mGroupList.add("e");
		
		mChildListContent.add("1");
		mChildListContent.add("2");
		mChildListContent.add("3");
		mChildListContent.add("4");
		mChildListContent.add("5");
		
		mChildList.add(mChildListContent);
		mChildList.add(mChildListContent);
		mChildList.add(mChildListContent);
		mChildList.add(mChildListContent);
		mChildList.add(mChildListContent);
		mChildList.add(mChildListContent);
		mChildList.add(mChildListContent);
		mChildList.add(mChildListContent);
		mChildList.add(mChildListContent);
		mChildList.add(mChildListContent);
		mChildList.add(mChildListContent);*/

		mBaseExpandableAdapter = new BaseExpandableAdapter(this, mGroupList, mChildList);
		mListView.setAdapter(mBaseExpandableAdapter);
		
		mListView.setGroupIndicator(null);
		
		int groupCount = (int) mBaseExpandableAdapter.getGroupCount();
		
		mListView.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				Toast.makeText(getApplicationContext(), "c click = " + childPosition, 
						Toast.LENGTH_SHORT).show();
				return false;
			}
		});//When user choose one item
		
		mListView.setOnGroupExpandListener(new OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int groupPosition) {
				selectedCategory = mGroupList.get(groupPosition);
				GetItemListTask getItemListTask = new GetItemListTask();
				getItemListTask.execute();
				
				int groupCount = mBaseExpandableAdapter.getGroupCount();
			
				for (int i = 0; i < groupCount; i++) {
					if (!(i == groupPosition))
						mListView.collapseGroup(i);
				}
			}
		});//When user expand group
        
	}
	
	private ExpandableListView mListView;

	private void setLayout(){
		mListView = (ExpandableListView) findViewById(R.id.expandLV);
	}
	
	private class GetItemListTask extends AsyncTask<Void, Void, Void>{

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				URL url = new URL("http://192.168.43.64:8080/Weening11evenServer/CommunicateServlet");
				URLConnection connection = url.openConnection();

				//send request to server
				connection.setDoOutput(true);
				OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
				out.write("For ItemList/" + selectedCategory);
				out.close();

				//receive result of login from server
				BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				String receivedData;
				
				while((receivedData = in.readLine()) != null){
					mChildListContent.add(receivedData);
				}
				in.close();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
		
	}
	
	private class GetCategoryTask extends AsyncTask<Void, Void, Void>{

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				URL url = new URL("http://192.168.43.64:8080/Weening11evenServer/CommunicateServlet");
				URLConnection connection = url.openConnection();

				//send request to server
				connection.setDoOutput(true);
				OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
				out.write("For Category/");
				out.close();

				//receive result of login from server
				BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				String receivedBrand;
				while((receivedBrand = in.readLine()) != null){
					mGroupList.add(receivedBrand);
				}
				in.close();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
	}
}
